package com.wee.cronos.client.core.view;

import java.util.ArrayList;

import com.google.gwt.cell.client.AbstractCell;
import com.google.gwt.cell.client.Cell;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.safehtml.shared.SafeHtmlBuilder;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.cellview.client.CellList;
import com.google.gwt.user.client.ui.Widget;
import com.gwtplatform.mvp.client.UiHandlers;
import com.gwtplatform.mvp.client.ViewWithUiHandlers;
import com.wee.cronos.client.core.presenter.ProjectExplorerPresenter.ProjectExplorerView;
import com.wee.cronos.client.resources.Resource;
import com.wee.cronos.client.ui.MouseAwareFlowPanel;
import com.wee.cronos.shared.ProjectDetails;

public class ProjectExplorerViewImpl extends ViewWithUiHandlers<ProjectExplorerViewImpl.ProjectExplorerUiHandlers> implements ProjectExplorerView
{
	protected static final ProjectExplorerUiBinder uiBinder = GWT.create(ProjectExplorerUiBinder.class);

	interface ProjectExplorerUiBinder extends UiBinder<Widget, ProjectExplorerViewImpl> {}
	
	public interface ProjectExplorerUiHandlers extends UiHandlers
	{	
		void fetchProjectInfo();
	}

	Widget widget;
	
	@UiField
	MouseAwareFlowPanel content;
	
	private static class ProjectDetailsCell extends AbstractCell<ProjectDetails> 
	{
		@Override
		public void render(Cell.Context context, ProjectDetails project, SafeHtmlBuilder sb) 
		{
			if (project != null) 
			{
				sb.appendEscaped(project.getName());
			}
		}
	}
	
	@UiField(provided = true)
	CellList<ProjectDetails> projectList;
	
	public ProjectExplorerViewImpl()
	{
		projectList = new CellList<ProjectDetails>(new ProjectDetailsCell());
	    widget = uiBinder.createAndBindUi(this);
	}
	
	@Override
	public Widget asWidget() 
	{
		return widget;
	}
	
	@UiHandler("content")
	public void onProjectHandlerClicked(ClickEvent event)
	{
		getUiHandlers().fetchProjectInfo();
	}

	@Override
	public void setProjectList(ArrayList<ProjectDetails> projectInfo) {
		projectList.setRowData(projectInfo);
	}
}
